package com.example.boardroom.demo;

import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.GsonUtils;
import com.example.boardroom.R;
import com.example.boardroom.demo.custom.ChartBean;
import com.example.boardroom.demo.custom.DayAxisValueFormatter;
import com.example.boardroom.demo.custom.DemoBase;
import com.example.boardroom.demo.custom.MyMarkerView;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.IFillFormatter;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.dataprovider.LineDataProvider;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.Utils;

import java.util.ArrayList;
import java.util.List;

public class MPAndroidChartActivity extends DemoBase {
    private LineChart chart, mLineChart;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mp);
        mLineChart = findViewById(R.id.chart1);
        xxx();
    }

    @Override
    protected void saveToGallery() {
        saveToGallery(chart, "LineChartActivity1");
    }

    private void xxx() {
        {   // // Chart Style // //
            chart = findViewById(R.id.chart1);

            // background color
            chart.setBackgroundColor(Color.WHITE);

            // disable description text
            chart.getDescription().setEnabled(false);

            // enable touch gestures
            chart.setTouchEnabled(true);

            // set listeners
            chart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
                @Override
                public void onValueSelected(Entry e, Highlight h) {

                }

                @Override
                public void onNothingSelected() {

                }
            });
            chart.setDrawGridBackground(false);

            // create marker to display box when values are selected
            MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker_view);

            // Set the marker to the chart
            mv.setChartView(chart);
            chart.setMarker(mv);

            // enable scaling and dragging
            chart.setDragEnabled(true);
            chart.setScaleEnabled(true);
            // chart.setScaleXEnabled(true);
            // chart.setScaleYEnabled(true);

            // force pinch zoom along both axis
            chart.setPinchZoom(true);

        }


//        XAxis xAxis;
        {   // // X-Axis Style // //
//            xAxis = chart.getXAxis();

            // vertical grid lines
//            xAxis.enableGridDashedLine(10f, 10f, 0f);
            XAxis xAxis = chart.getXAxis();
            ValueFormatter valueFormatter=new ValueFormatter() {
                @Override
                public String getFormattedValue(float value) {
//                    return super.getFormattedValue(value);
                    return value+":00";
                }
            };
            xAxis.setValueFormatter(valueFormatter);
            xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
            xAxis.setTypeface(tfLight);
            xAxis.setAxisLineColor(getResources().getColor(R.color.holo_red_dark));
            xAxis.setTextColor(getResources().getColor(R.color.holo_green_light));
            xAxis.setDrawGridLines(false);
            xAxis.setGranularity(1f); // only intervals of 1 day
//            xAxis.setLabelCount(7);
//            xAxis.setValueFormatter(xAxisFormatter);
        }

        YAxis yAxis;
        {   // // Y-Axis Style // //
            yAxis = chart.getAxisLeft();
            yAxis.setDrawGridLines(true);
            // disable dual axis (only use LEFT axis)
            chart.getAxisRight().setEnabled(false);

            // horizontal grid lines
//            yAxis.enableGridDashedLine(10f, 10f, 0f);
//            yAxis.setLabelCount(7);
            // axis range
            yAxis.setAxisMaximum(200f);
            yAxis.setAxisMinimum(-50f);
        }


        {   // // Create Limit Lines // //


//            LimitLine ll1 = new LimitLine(150f, "Upper Limit");
//            ll1.setLineWidth(4f);
//            ll1.enableDashedLine(10f, 10f, 0f);
//            ll1.setLabelPosition(LimitLine.LimitLabelPosition.RIGHT_TOP);
//            ll1.setTextSize(10f);
//            ll1.setTypeface(tfRegular);


            // draw limit lines behind data instead of on top
            yAxis.setDrawLimitLinesBehindData(false);
//            xAxis.setDrawLimitLinesBehindData(true);

            // add limit lines
//            yAxis.addLimitLine(ll1);
            //xAxis.addLimitLine(llXAxis);
        }

        setData(45, 180);

        // draw points over time
//        chart.animateX(1500);

        // get the legend (only possible after setting data)
//        Legend l = chart.getLegend();

        // draw legend entries as lines
//        l.setForm(Legend.LegendForm.LINE);

//        List<ILineDataSet> sets = chart.getData()
//                .getDataSets();
//
//        for (ILineDataSet iSet : sets) {
//
//            LineDataSet set = (LineDataSet) iSet;
//            set.setMode(set.getMode() == LineDataSet.Mode.CUBIC_BEZIER
//                    ? LineDataSet.Mode.LINEAR
//                    :  LineDataSet.Mode.CUBIC_BEZIER);
//        }
//        chart.invalidate();
    }

    private ArrayList<Entry> getxxx() {

        ArrayList<Entry> values = new ArrayList<>();


        String data="{\"code\":200,\"msg\":null,\"data\":[{\"key\":\"00:00\",\"value\":0.0},{\"key\":\"01:00\",\"value\":0.0},{\"key\":\"02:00\",\"value\":0.0},{\"key\":\"03:00\",\"value\":0.0},{\"key\":\"04:00\",\"value\":0.0},{\"key\":\"05:00\",\"value\":0.0},{\"key\":\"06:00\",\"value\":0.0},{\"key\":\"07:00\",\"value\":0.0},{\"key\":\"08:00\",\"value\":0.0},{\"key\":\"09:00\",\"value\":29.3},{\"key\":\"10:00\",\"value\":0.0},{\"key\":\"11:00\",\"value\":0.0},{\"key\":\"12:00\",\"value\":0.0},{\"key\":\"13:00\",\"value\":0.0},{\"key\":\"14:00\",\"value\":0.0},{\"key\":\"15:00\",\"value\":0.0},{\"key\":\"16:00\",\"value\":0.0},{\"key\":\"17:00\",\"value\":0.0},{\"key\":\"18:00\",\"value\":0.0},{\"key\":\"19:00\",\"value\":0.0},{\"key\":\"20:00\",\"value\":0.0},{\"key\":\"21:00\",\"value\":0.0},{\"key\":\"22:00\",\"value\":0.0},{\"key\":\"23:00\",\"value\":0.0}]}";
        ChartBean chartBean = GsonUtils.fromJson(data, ChartBean.class);
//        for (ChartBean.DataDTO datum : chartBean.data) {
////            float ss= datum.value;
//            values.add(new Entry(Float.parseFloat(datum.key),  datum.value));
//        }
//        int x=0;
//        for (int i = 0; i < chartBean.data.size(); i++) {
//            values.add(new Entry(x,   chartBean.data.get(i).value));
//            x=x+10;
//        }
        values.add(new Entry(10, 50));
        values.add(new Entry(20, 70));
        values.add(new Entry(30, 60));
        values.add(new Entry(40, 50));
        values.add(new Entry(50, 100));
        values.add(new Entry(60, 50));
        values.add(new Entry(70, 60));

        values.add(new Entry(80, 50));
        values.add(new Entry(10, 50));
        values.add(new Entry(20, 70));
        values.add(new Entry(30, 60));
        values.add(new Entry(40, 50));
        values.add(new Entry(50, 100));
        values.add(new Entry(60, 50));
        values.add(new Entry(70, 60));
        values.add(new Entry(80, 50));

        values.add(new Entry(10, 50));
        values.add(new Entry(20, 70));
        values.add(new Entry(30, 60));
        values.add(new Entry(40, 50));
        values.add(new Entry(50, 100));
        values.add(new Entry(60, 50));
        values.add(new Entry(70, 60));
        values.add(new Entry(80, 50));

        return values;
    }

    private void setData(int count, float range) {

//        ArrayList<Entry> values = new ArrayList<>();
//
//        for (int i = 0; i < count; i++) {
//
//            float val = (float) (Math.random() * range) - 30;
//            values.add(new Entry(i, val));
//        }

        LineDataSet set1;
        if (chart.getData() != null &&
                chart.getData().getDataSetCount() > 0) {
            set1 = (LineDataSet) chart.getData().getDataSetByIndex(0);
            set1.setValues(getxxx());
            set1.notifyDataSetChanged();
            chart.getData().notifyDataChanged();
            chart.notifyDataSetChanged();
        } else {
            // create a dataset and give it a type
            set1 = new LineDataSet(getxxx(), "DataSet 1");

            set1.setMode(LineDataSet.Mode.CUBIC_BEZIER);
            set1.setDrawIcons(false);

            // draw dashed line
//            set1.enableDashedLine(10f, 5f, 0f);

            // black lines and points
            set1.setColor(getResources().getColor(R.color.colorFF02D8FF));
            set1.setCircleColor(getResources().getColor(R.color.colorFF02D8FF));

            // line thickness and point size
            set1.setLineWidth(1f);
            set1.setCircleRadius(3f);

            // draw points as solid circles
            set1.setDrawCircleHole(false);

            // customize legend entry
//            set1.setFormLineWidth(1f);
//            set1.setFormLineDashEffect(new DashPathEffect(new float[]{10f, 5f}, 0f));
//            set1.setFormSize(15.f);

            // text size of values
            set1.setValueTextSize(9f);

            // draw selection line as dashed
//            set1.enableDashedHighlightLine(10f, 5f, 0f);

            // set the filled area
            set1.setDrawFilled(true);
//            set1.setFillFormatter(new IFillFormatter() {
//                @Override
//                public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider) {
//                    return chart.getAxisLeft().getAxisMinimum();
//                }
//            });

            // set color of filled area
            if (Utils.getSDKInt() >= 18) {
                // drawables only supported on api level 18 and above
                Drawable drawable = ContextCompat.getDrawable(this, R.drawable.fade_red);
                set1.setFillDrawable(drawable);
            } else {
                set1.setFillColor(Color.BLACK);
            }

            ArrayList<ILineDataSet> dataSets = new ArrayList<>();
            dataSets.add(set1); // add the data sets

            // create a data object with the data sets
            LineData data = new LineData(dataSets);
            chart.setDrawGridBackground(false);
            // set data
            chart.setData(data);
        }
    }

}
